// // 输入tableData示例
// const tableData = {
//     name: '导出excel表',
//     data: [
//         {
//             io_date: '2024-06-13',
//             io_stock_loc: 'F4024',
//             io_num: 'FF4566288966',
//             io_code: 'CP7562366',
//             io_desc: '物料描述',
//             io_quantity: 2
//         }
//     ],
//     fields: [
//         { header: '出库日期', key: 'io_date', width: 12 },
//         { header: '库房', key: 'io_stock_loc', width: 10 },
//         { header: '出库单号', key: 'io_num', width: 20 },
//         { header: '备件编码', key: 'io_code', width: 20 },
//         { header: '物料描述', key: 'io_desc', width: 50 },
//         { header: '出库数量', key: 'io_quantity', width: 12 }
//     ]
// }
import ExcelJS from 'exceljs'
import FileSaver from 'file-saver'
export default {
    //表格数据导出excel
    async exportExcel(tableData) {
        // 模拟获取数据
        let allData = [...tableData.data]
        let length = allData.length
        // 创建工作簿
        const workbook = new ExcelJS.Workbook()
        // 添加sheet第一行冻结的工作表,冻结列 xSplit: 7, 冻结行 ySplit: 1
        const worksheet = workbook.addWorksheet(tableData.name, {
            // views: [{ state: 'frozen', xSplit: 5, ySplit: 1 }]
        })
        // 设置 sheet 的默认行高
        worksheet.properties.defaultRowHeight = 25
        // 设置列
        worksheet.columns = tableData.fields
        //设置列居中
        worksheet.columns.forEach((item, i) => {
            worksheet.getColumn(i + 1).alignment = { vertical: 'middle', horizontal: 'center' }
        })
        // 添加行
        worksheet.addRows(allData)
        // 设置表头样式
        let headerRow = worksheet.getRow(1)
        headerRow.eachCell((cell) => {
            cell.fill = {
                type: 'pattern',
                pattern: 'solid',
                fgColor: { argb: 'dde0e7' }
            }
            cell.font = {
                name: '等线',
                color: { argb: '00000000' },
                family: 2,
                size: 10,
                bold: true
            }
            cell.alignment = { vertical: 'middle', horizontal: 'center' }
        })
        // 获取最长单元列序号
        function getLetterByIndex(index) {
            index--; // 转换为0基索引
            let letters = '';
            while (index >= 0) {
                letters = String.fromCharCode((index % 26) + 65) + letters; // 65是A的ASCII码
                index = Math.floor(index / 26) - 1; // 计算下一个字母
            }
            return letters;
        }
        let to = getLetterByIndex(tableData.fields.length)
        // 设置自动筛选器
        worksheet.autoFilter = {
            from: 'A1',
            to: to + '1'
        }
        // // 锁定工作表，整张表，后续个别在解锁
        // await worksheet.protect('the-password', {
        //     autoFilter: true,
        //     selectLockedCells: true //true为单元格可选择
        // })

        // 遍历单元格并根据内容设置背景颜色
        worksheet.eachRow((row, rowNumber) => {
            // 设置行高
            row.height = 25
        })
        function getCurrentTime() {
            const now = new Date();
            const year = now.getFullYear();
            const month = String(now.getMonth() + 1).padStart(2, '0'); // 月份从0开始
            const day = String(now.getDate()).padStart(2, '0');
            const hours = String(now.getHours()).padStart(2, '0');
            const minutes = String(now.getMinutes()).padStart(2, '0');
            const seconds = String(now.getSeconds()).padStart(2, '0');

            return `${year}${month}${day}${hours}${minutes}${seconds}`;
        }
        // 导出excel
        workbook.xlsx.writeBuffer().then((buffer) => {
            FileSaver.saveAs(new Blob([buffer], { type: 'application/octet-stream' }), `${tableData.name}_${getCurrentTime()}.xlsx`)
        })

    }
}